package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyProductAddBo;
import com.ruoyi.system.bo.ZsyyProductQueryBo;
import com.ruoyi.system.bo.ZsyyProductEditBo;
import com.ruoyi.system.domain.ZsyyProduct;
import com.ruoyi.system.mapper.ZsyyProductMapper;
import com.ruoyi.system.vo.ZsyyProductVo;
import com.ruoyi.system.service.IZsyyProductService;
import com.ruoyi.system.mapper.ZsyyProductMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 服务产品管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyProductServiceImpl extends ServicePlusImpl<ZsyyProductMapper, ZsyyProduct> implements IZsyyProductService {
    @Autowired
    private ZsyyProductMapper iZsyyProductMapper;

    @Override
    public ZsyyProductVo queryById(Long id){
        return getVoById(id, ZsyyProductVo.class);
    }

    @Override
    public TableDataInfo<ZsyyProductVo> queryPageList(ZsyyProductQueryBo bo) {
        PagePlus<ZsyyProduct, ZsyyProductVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyProductVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyProductVo> queryList(ZsyyProductQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyProductVo.class);
    }

    private LambdaQueryWrapper<ZsyyProduct> buildQueryWrapper(ZsyyProductQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyProduct> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), ZsyyProduct::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getDigest()), ZsyyProduct::getDigest, bo.getDigest());
        lqw.eq(bo.getCourseId() != null, ZsyyProduct::getCourseId, bo.getCourseId());
        lqw.eq(bo.getParkId() != null, ZsyyProduct::getParkId, bo.getParkId());
        lqw.eq(StrUtil.isNotBlank(bo.getDetailUrl()), ZsyyProduct::getDetailUrl, bo.getDetailUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getPicUrl()), ZsyyProduct::getPicUrl, bo.getPicUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getAttribute()), ZsyyProduct::getAttribute, bo.getAttribute());
        lqw.eq(bo.getStock() != null, ZsyyProduct::getStock, bo.getStock());
        lqw.eq(bo.getPrice() != null, ZsyyProduct::getPrice, bo.getPrice());
        lqw.eq(bo.getPreferentialPrice() != null, ZsyyProduct::getPreferentialPrice, bo.getPreferentialPrice());
        lqw.eq(bo.getType() != null, ZsyyProduct::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getProductDetail()), ZsyyProduct::getProductDetail, bo.getProductDetail());
        lqw.eq(bo.getSellNum() != null, ZsyyProduct::getSellNum, bo.getSellNum());
        lqw.eq(bo.getDuration() != null, ZsyyProduct::getDuration, bo.getDuration());
        lqw.eq(bo.getStatus() != null, ZsyyProduct::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyProductAddBo bo) {
        ZsyyProduct add = BeanUtil.toBean(bo, ZsyyProduct.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyProductEditBo bo) {
        ZsyyProduct update = BeanUtil.toBean(bo, ZsyyProduct.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
